<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

class Article extends Model
{
    /**
     * 表名
     *
     * @var string
     */
    protected $table = "article";

    /**
     * 填充字段
     *
     * @var array
     */
    protected $fillable = [
        'id',
        'title',
        'intro',
        'status',
        'author_id',
        'content',
        'pub_time',
    ];

    const FORMAL_STATUS = 1;

    /**
     * 根据状态获取文章条数文章
     *
     * @param $query
     * @param $status
     * @return mixed
     */
    public function scopeStatus($query){
        return $query->where('status', self::FORMAL_STATUS);
    }

    public function scopeAdmin($query)
    {
        if( Auth::user()->is_admin )
        {
            return $query;
        }
        return $query->where('author_id', Auth::user()->id);
    }
}
